package com.zkh360.service.message.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zkh360.service.message.constant.ErrorCode;
import com.zkh360.service.message.domain.SysAuthorities;
import com.zkh360.service.message.domain.SysRoles;
import com.zkh360.service.message.domain.SysRolesAuthorities;
import com.zkh360.service.message.exceptions.BusinessException;
import com.zkh360.service.message.mapper.AuthoritiesMapper;
import com.zkh360.service.message.mapper.RoleMapper;
import com.zkh360.service.message.mapper.RolesAuthorityMapper;
import com.zkh360.service.message.model.SysAuthorityVO;
import com.zkh360.service.message.model.SysRoleVO;
import com.zkh360.service.message.model.SysRolesAuthorityVO;
import com.zkh360.service.message.model.SysUserVO;
import com.zkh360.service.message.service.AuthorityService;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 
 * @author chuanxi.zhao
 *
 */
@Service(value = "AuthorityService")
@Slf4j
public class AuthorityServiceImpl implements AuthorityService {

	@Autowired
	private AuthoritiesMapper authoritiesMapper;

	@Autowired
	private RoleMapper roleMappper;

	@Autowired
	private RolesAuthorityMapper rolesAuthorityMapper;

	@Override
	public List<SysAuthorityVO> getAuthorities(SysAuthorityVO sysAuthorityVo) throws BusinessException {

		SysAuthorities sysUsersParams = new SysAuthorities();
		sysUsersParams.setAuthority_id(sysAuthorityVo.getAuthority_id());
		sysUsersParams.setAuthority_name(sysAuthorityVo.getAuthority_name());
		List<SysAuthorities> sysAuthoritiesList = authoritiesMapper.select(sysUsersParams);
		List<SysAuthorityVO> sysAuthorityList = new ArrayList<>();
		sysAuthoritiesList.stream().forEach(sysAuthoritiesTemp -> {
			SysAuthorityVO sysAuthorityVoTemp = new SysAuthorityVO();
			BeanUtils.copyProperties(sysAuthoritiesTemp, sysAuthorityVoTemp);
			sysAuthorityList.add(sysAuthorityVoTemp);
		});

		return sysAuthorityList;
	}

	@Override
	public List<SysAuthorityVO> getAuthoritysByUser(SysUserVO sysUserVo) throws BusinessException {
		log.info("根据用户获取权限");
		List<SysAuthorities> sysAuthoritiesList = authoritiesMapper.getAuthoritiesByUser(sysUserVo.getUser_id(),
				sysUserVo.getUser_account());
		List<SysAuthorityVO> sysAuthorityList = new ArrayList<>();
		sysAuthoritiesList.stream().forEach(sysAuthoritiesTemp -> {
			SysAuthorityVO sysAuthorityVoTemp = new SysAuthorityVO();
			BeanUtils.copyProperties(sysAuthoritiesTemp, sysAuthorityVoTemp);
			sysAuthorityList.add(sysAuthorityVoTemp);
		});

		return sysAuthorityList;
	}

	@Override
	public void addAuthority(SysAuthorityVO sysAuthorityVo) {
		SysAuthorities sysUsersParams = new SysAuthorities();
		sysUsersParams.setAuthority_desc(sysAuthorityVo.getAuthority_desc());
		sysUsersParams.setAuthority_id(sysAuthorityVo.getAuthority_id());
		sysUsersParams.setAuthority_name(sysAuthorityVo.getAuthority_name());
		sysUsersParams.setEnabled(sysAuthorityVo.getEnabled());
		sysUsersParams.setIssys(sysAuthorityVo.getIssys());

		authoritiesMapper.insert(sysUsersParams);
	}

	@Override
	public void deleteAuthority(Integer authorityId) {
		// 需要判断所有的角色都没有配置该权限才成删除
		List<SysRoles> roles = roleMappper.getRoleByAuthorityId(authorityId, 1);
		if (roles.size() > 0) {
			throw BusinessException.customException(ErrorCode.CODE_FAILURE, "存在角色配置该权限，无法删除");
		} else {
			SysAuthorities searchParams = new SysAuthorities();
			searchParams.setAuthority_id(authorityId);
			SysAuthorities sysAuthorities = authoritiesMapper.selectOne(searchParams);
			sysAuthorities.setEnabled(0);
			authoritiesMapper.updateByPrimaryKey(sysAuthorities);
		}
	}

	@Override
	public void editAuthority(SysAuthorityVO sysAuthorityVo) {

		SysAuthorities searchParams = new SysAuthorities();
		searchParams.setAuthority_id(sysAuthorityVo.getAuthority_id());
		SysAuthorities sysAuthorities = authoritiesMapper.selectOne(searchParams);
		if (sysAuthorities == null) {
			throw BusinessException.customException(ErrorCode.CODE_FAILURE, "查询不到该权限信息");
		} else {
			sysAuthorities.setAuthority_desc(sysAuthorityVo.getAuthority_desc());
			sysAuthorities.setAuthority_name(sysAuthorityVo.getAuthority_name());
			sysAuthorities.setEnabled(sysAuthorityVo.getEnabled());
			sysAuthorities.setIssys(sysAuthorityVo.getIssys());

			authoritiesMapper.updateByPrimaryKey(sysAuthorities);
		}
	}

	@Override
	public List<SysAuthorityVO> getAuthoritysByRole(SysRoleVO sysRoleVo) {
		log.info("根据角色获取权限");
		List<SysAuthorities> sysAuthoritiesList = authoritiesMapper.getAuthoritiesByRole(sysRoleVo.getRole_id());
		List<SysAuthorityVO> sysAuthorityList = new ArrayList<>();
		sysAuthoritiesList.stream().forEach(sysAuthoritiesTemp -> {
			SysAuthorityVO sysAuthorityVoTemp = new SysAuthorityVO();
			BeanUtils.copyProperties(sysAuthoritiesTemp, sysAuthorityVoTemp);
			sysAuthorityList.add(sysAuthorityVoTemp);
		});

		return sysAuthorityList;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void ediAuthorityByRole(List<SysRolesAuthorityVO> sysRolesAuthorityVoList) {
		// 清除原来的客户权限资源信息
		SysRolesAuthorities sysRolesAuthoritiesParams = new SysRolesAuthorities();
		sysRolesAuthoritiesParams.setRole_id(sysRolesAuthorityVoList.get(0).getRole_id());
		rolesAuthorityMapper.delete(sysRolesAuthoritiesParams);
		// 查询所有资源数据，方便后面找到父节点
		List<SysRolesAuthorities> sysRolesAuthorityList = new ArrayList<>();
		sysRolesAuthorityVoList.forEach(sysRolesAuthorityVo -> {
			SysRolesAuthorities temp = new SysRolesAuthorities();
			temp.setRole_id(sysRolesAuthorityVo.getRole_id());
			temp.setAuthority_id(sysRolesAuthorityVo.getAuthority_id());
			temp.setEnabled(sysRolesAuthorityVo.getEnabled());
			sysRolesAuthorityList.add(temp);
		});
		rolesAuthorityMapper.insertList(sysRolesAuthorityList);
	}

}
